#include "stdafx.h"
#include ".\screen_mgr.h"
#include "iltdrawprim.h"
#include "..\clientinterfaces.h"
#include "pageInfoGetter.hpp"
#include "screen.h"
screen_mgr::screen_mgr(void):
m_screens_file("Screen/screen.txt"),
m_page_name("screens"),
m_screen_width(800),
m_screen_height(600)
{
	//init this draw prim's args
	g_pLTCDrawPrim->SetTransformType(DRAWPRIM_TRANSFORM_SCREEN);
	g_pLTCDrawPrim->SetColorOp(DRAWPRIM_MODULATE);
	g_pLTCDrawPrim->SetAlphaBlendMode(DRAWPRIM_BLEND_MOD_SRCALPHA);
	g_pLTCDrawPrim->SetZBufferMode(DRAWPRIM_NOZ);
	g_pLTCDrawPrim->SetAlphaTestMode(DRAWPRIM_NOALPHATEST);
	g_pLTCDrawPrim->SetClipMode(DRAWPRIM_FASTCLIP);
	g_pLTCDrawPrim->SetFillMode(DRAWPRIM_FILL);
	g_pLTCDrawPrim->SetCullMode(DRAWPRIM_CULL_NONE);
	//g_pLTCDrawPrim->SetCamera(NULL);
	

}

screen_mgr::~screen_mgr(void)
{
	for (m_screen_it=m_screens.begin();m_screen_it!=m_screens.end();++m_screen_it)
	{
		delete (*m_screen_it).second;
	}
	m_screens.clear();
}

screen_mgr& screen_mgr::single()
{
	static screen_mgr _single;
	return _single;
}

void screen_mgr::render()
{
	g_pLTCDrawPrim->BeginDrawPrim();
	g_pLTCDrawPrim->SetTransformType(DRAWPRIM_TRANSFORM_SCREEN);
	for (m_screen_it=m_screens.begin();m_screen_it!=m_screens.end();++m_screen_it)
	{
		if( (*m_screen_it).second->can_show())
		{
			(*m_screen_it).second->render();
		}
	}
	g_pLTCDrawPrim->EndDrawPrim();


}

void screen_mgr::load_screen_size( int w,int h )
{
	m_screen_width=w;
	m_screen_height=h;
	screen::set_screen_size(w,h);
}

void screen_mgr::load_screens()
{

	//load screens
	//Utility::page_info_getter::Instance().add_page("rez/"+m_screens_file,m_page_name);
	m_size=Utility::page_info_getter::Instance().get(m_page_name,"info","size",m_size);
	for (int i=0;i<m_size;++i)
	{
		static std::string tempstr;
		char tempgroup[10];
		sprintf(tempgroup,"screen%d",i);
		std::string temp_file_name;
		std::string temp_name;
		temp_file_name=Utility::page_info_getter::Instance().get(m_page_name,tempgroup,"filename",temp_file_name);
		temp_name=Utility::page_info_getter::Instance().get(m_page_name,tempgroup,"name",temp_name);
		screen* tempscreen=new screen;
		tempscreen->load(temp_file_name);
		m_screens.insert(make_pair(temp_name,tempscreen));
	}


}

void screen_mgr::set_screen_show( std::string name,bool show )
{
	std::map<std::string,screen*>::iterator tempit;
	tempit=m_screens.find(name);
	if (tempit!=m_screens.end())
	{
		(*tempit).second->set_show(show);
	}


}

void screen_mgr::disable_show_all_screen()
{
	for (m_screen_it=m_screens.begin();m_screen_it!=m_screens.end();++m_screen_it)
	{
		(*m_screen_it).second->set_show(false);
	}


}